% This functions computes, for the model with free entry where fe is such that N=1, 
% the same outcomes as the GEobjects functions, and adds:

% - Fixed cost of entry measured in tradables (fe)
% - Equilibrium mass of firms (Nimplied), mechanically equal to 1

% See Model with Free Entry Appendix

function [fe,Nimplied,SP,SP2,P,RX,M,Iw, sN, sM,Ptot,LN] = GEobjects_FE_pre(w,sD, sX, phi,fc,fx, param)

g = param.gamma;
eps = param.eps;
sg = param.sg;
eta = param.eta;
beta = param.beta;
qD = param.qD;
theta = param.theta;
vartheta = param.vartheta;
aN = param.aN;
AN = param.AN;
zeta = param.zeta;
varrho = param.varrho;
pM = param.pM;
T = param.T;
FXM = param.FXM; FX = param.FX; FM = param.FM;

pMw = pM / w;

% Constants:

k1 =  qD^(g*(sg-1)) * (1-g)^((1-g)*(sg-1)) * g^(g*(sg-1)) * g * (sg-1)^sg/sg^sg * eta;  % kappa2_wiggle
k2 = qD^(g*(sg-1)) * (sg-1)^(sg-1)/sg^sg * (1-g)^((1-g)*(sg-1))* g^(g*(sg-1)); % kappa3
k3 = k2 * (theta-1)/theta; % kappa3*times (theta-1)/theta
k5 = (sg / (sg-1))^(1-sg) * (1-g)^((1-g)*(sg-1)) * (g*beta^(eps/(eps-1))*qD)^(g*(sg-1)); % kappa1
k6 = mean(k1*(sD.^(-1)-1).^(1/(eps-1)/eta) .* fc + k2 .* ((sD<1).*(sX==0)) * FM + k3 * (sX./(1-sX)).^(theta/(theta-1)) .*fx + k2 * ((sX>0).*(sD==1)) * FX + k2 * ((sX>0).*(sD<1)) * FXM ); % \varPsi
k7 = mean(phi.^(sg-1) .* (sX./(1-sX)) .*sD.^(-g*(sg-1)/(eps-1))); % \Upsilon
k8 = mean(phi.^(sg-1) .* (1./(1-sX)) .*sD.^(1-g*(sg-1)/(eps-1))); % \Gamma
Lambda = ((sg/ (sg-1))^(1/(1-g)) * (1-g)^(-1) * (g*beta^(eps/(eps-1))*qD)^(-g/(1-g)))^((sg-1)*(1-g)) *  (mean(phi.^(sg-1).*sD.^(-g*(sg-1)/(eps-1))))^(-1); % \Lambda; new term in free entry model
Nimplied = 1;

% Price indeces and expenditure shares:

P = Nimplied.^(-1/((sg-1)*(1-g))) * (sg/ (sg-1))^(1/(1-g)) * (1-g)^(-1) * (g*beta^(eps/(eps-1))*qD)^(-g/(1-g)) * (mean(phi.^(sg-1).*sD.^(-g*(sg-1)/(eps-1))))^(1/((1-sg)*(1-g))); % price index of locally-produced manufacturing bundle measured in domestic labor (P/w)
Pw_power = 1/Nimplied  * Lambda;% for use below
PM = (varrho^zeta * pMw^(1-zeta) + (1-varrho)^zeta * P^(1-zeta)).^(1/(1-zeta)); % price index of manufacturing bundle measured in domestic labor (PM/w) 
pNw = 1/AN; % price of non-tradable good measured in domestic labor (pN/w)
xx = PM/pNw; % relative price of manufacturing bundle to non-tradable good
sN = aN^vartheta ./ (aN^vartheta + (1-aN)^vartheta *xx.^(1-vartheta)); % expenditure share on non-tradable
sM = varrho^zeta ./ (varrho^zeta + (1-varrho)^zeta *(P/pMw).^(1-zeta))  ; % expenditure share on imported final good
aT = (1-sN)* (1- sM); % share of expenditure on locally-produced manufacturing bundle (alpha wiggle in paper's notation)

% Obtain S/w associated with consumer budget constraint:
Sw = 1 / Nimplied * (param.L + param.T) / ( 1/aT * (1 - (sg-1)/sg * g *k5 *k8 * P^((sg-1)*(1-g))));
% Obtain S/w associated with labor market clearing:
Sw2 = 1 / Nimplied * param.L * ( (1 +  (sg-1) * (1-g)) * ( 1/sg+ k5 * k7 * Pw_power / sg) +  sN/aT * (1 - g * (sg-1)/sg *k5*k8 * Pw_power) ).^(-1);

% Obtain fe from the free entry condition under N=1
fe = w *  (1/sg  + k5 * k7 * Lambda / sg - Lambda * k6) * Sw; 

SX = g* (sg-1)/sg * k5* P^((sg-1)*(1-g)) * k8 * Sw*Nimplied; % expenditure in local inputs by local firms measured in domestic labor (SX/w)
RX = k5  * P^((sg-1)*(1-g)) * Sw *k7*Nimplied; % aggregate exports measured in domestic labor (RX/w)
M = g * (sg-1)/sg * (Sw + RX) - SX; % imports of inputs measured in domestic labor (M/w)
Iw = param.L + T ; % consumer income measured in domestic labor (I/w); note: with free entry net profits are zero
Ptot = (aN^vartheta .* pNw.^(1-vartheta) + (1-aN)^vartheta *PM.^(1-vartheta)).^(1/(1-vartheta)) ; % overall consumer price index (CES price index of the non-tradable price and the price index of the manufacturing bundle) measured in domestic labor
LN =  sN/aT * ( 1 - (sg-1)/sg * g *k5 *k8 * P^((sg-1)*(1-g)) )*Sw*Nimplied; % labor in non-tradable
P = P*w; SP = Sw*w; SP2 = Sw2*w; Ptot = Ptot *w;
end